package xcmg.device.service.order;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import xcmg.device.dao.entity.order.SubcontractingOrderDO;
import xcmg.device.dao.entity.order.SubcontractingOrderDetailDO;
import xcmg.device.dao.mapper.order.SubcontractingOrderMapper;
import xcmg.device.infra.BeanUtil;
import xcmg.device.infra.enums.BillDesc;
import xcmg.device.service.GenDocumentService;
import xcmg.device.service.basic.CommonGenService;
import xcmg.device.service.vo.order.GpsMmSwOrderVO;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * SubcontractingOrder 后台接口实现类
 */
@Slf4j
@Service
public class SubcontractingOrderAddService {

    @Autowired
    private SubcontractingOrderMapper subcontractingOrderMapper;

    @Autowired
    private SubcontractingOrderDetailService subcontractingOrderDetailService;

    @Autowired
    private GpsMmSwOrderService gpsMmSwOrderService;
    @Autowired
    private CommonGenService commonGenService;
    @Autowired
    private GenDocumentService genDocumentService;

    private boolean checkAbleToCreateZbd(Long orderId) {
        List<Long> ids = subcontractingOrderMapper.checkAbleToCreateZbd(orderId);
        return ids.size() > 0;
    }

    @Transactional
    public void createZbdByBarcode(Long orderId) {
        GpsMmSwOrderVO order = gpsMmSwOrderService.getById(orderId);
        if (null != order && !CollectionUtils.isEmpty(order.getOrderDetailList())) {
            boolean flag = checkAbleToCreateZbd(order.getOrderId());
            if (!flag) {
                log.info("该订单不符合生成转包单条件，order:{}", order);
                return;
            }
            Date now = new Date();
            SubcontractingOrderDO zbdDO = BeanUtil.do2bo(order, SubcontractingOrderDO.class);
            zbdDO.setSupplierOrgId(order.getOrgId());
            //所属组织，入库时候确定
            zbdDO.setOrgId(null);
            zbdDO.setId(commonGenService.textIdWithException());
            zbdDO.setOrderId(orderId);
            zbdDO.setState(0);
            zbdDO.setDeliveryState(0);
            zbdDO.setCode(genDocumentService.genDocumentNoSeq(BillDesc.ZBD));
            zbdDO.setCreatedTime(now);
            zbdDO.setUpdatedTime(now);
            List<SubcontractingOrderDetailDO> details = order.getOrderDetailList().stream().map(orderDetail -> {
                SubcontractingOrderDetailDO detail = BeanUtil.do2bo(orderDetail, SubcontractingOrderDetailDO.class);
                detail.setId(commonGenService.textIdWithException());
                detail.setFid(zbdDO.getId());
                detail.setOrderDetailId(orderDetail.getOrderDetailId());
                detail.setRealAmount(BigDecimal.ZERO);
                detail.setZbAmount(BigDecimal.ZERO);
                detail.setCreatedTime(now);
                detail.setUpdatedTime(now);
                return detail;
            }).collect(Collectors.toList());
            subcontractingOrderDetailService.batchInsert(details);
            subcontractingOrderMapper.save(zbdDO);
        }
    }
}
